//
//  Array+Extension.swift
//  ZYA
//
//  Created by Ken on 2019/8/13.
//  Copyright © 2019 zwb. All rights reserved.
//

import UIKit

class Array_Extension: NSObject {

}

/**
 将获取到的数组与原先的数组合并成新的数组
 
 - parameter originalData:      原始数组.
 - parameter loadData:          加载得到的数组.
 - parameter transform:         合并条件.
 
 - returns: 返回新的数组用于tableView的数据源.
 */
public func combinedLoadData<T>(_ originalData: [[T]], loadData: [[T]], transform: (_ x1: [T]?, _ x2: [T]) -> Bool) -> [[T]] {
   
    
    var oData = originalData
    var lData = loadData
    
    guard lData.count > 0 else {
        return oData
    }
    // 比较原始数组的最后成员与加载数组的第一成员的属性值是否相同
    if transform(oData.last, lData.first!) {
        let firstOfloadData = lData.removeFirst()
        let lastOfOriginalData = oData.removeLast()
        
        // 如果两个属性值相同，组成新的数组存在原始数组的最后
        oData.append(lastOfOriginalData + firstOfloadData)
    }
    
    oData += lData
    return oData
}
///  一维数组 根据规则生成二位数组
public func generateTwo<T>(_ data: [T], transform: (_ x1: T, _ x2: T) -> Bool) -> [[T]] {
    
    var objects = [[T]]()
    
    for object in data {
        var flag = true
        for i in 0..<objects.count {
            
            guard let combinedObject = objects[i].first else {
                assert(objects.first?.first != nil, "Index out of range")
                continue
            }
            // 根据传入规则，匹配到就加到想同的数组
            if transform(object, combinedObject) {
                objects[i].append(object)
                flag = false
                continue
            }
        }
        // 首次或者 没有匹配到的时候，新加一个数组
        if flag || objects.count == 0 {
            objects.append([object])
        }
    }
    return objects
}
extension Array {
    // 去重 
    func filterDuplicates<E: Equatable>(_ filterX: (Element) -> E) -> [Element] {
        var result = [Element]()
        for value in self {
            let key = filterX(value)
            if !result.map({filterX($0)}).contains(key) {
                result.append(value)
            }
             
        }
        return result
    }
    // 查重   返回重复的数组
    func findDuplicates<E: Equatable>(_ filterX: (Element) -> E) -> [Element] {
        var result = [Element]()
        var dupArr = [Element]()
        for value in self {
            let key = filterX(value)
            if !result.map({filterX($0)}).contains(key) {
                result.append(value)
            } else {
                dupArr.append(value)
             }
         }
        return dupArr
    }
    
    // 删除
    @discardableResult
    mutating func removeWithClo(_ compareX: (Element) -> Bool) -> [Element] {
        var result = self
        for i in 0..<self.count {
            if compareX(self[i]) {
                result.remove(at: i)
            }
         }
        self = result
        return self
     }
    
    // 获取特定元素的坐标
    func getIndexFor(_ condition: (Element) -> Bool) -> Int? {
        var index:Int? = nil
        for i in 0..<self.count {
            if condition(self[i]){
                index = i
            }
          }
          return index
    }

 
    func prefix(count: Int) -> [Any]? {
        
        if count > self.count {
            return self
        }
        
        var tmpArr = [Any]()
        for (idx, item) in self.enumerated() {
            if idx < count {
                tmpArr.append(item)
            }
        }
        return tmpArr
    }
}
